package br.com.eres.ui.adapters;

import java.util.List;

import br.com.carcara.scc.R;
import br.com.eres.control.Sessao;
import br.com.eres.model.Mesa;

import android.content.Context;
import android.graphics.Color;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MesaAdapter extends BaseAdapter {
	
	List<Mesa> listaMesas;
	private LayoutInflater mInflater;
	private ViewHolder holder;
	private int tempoAviso;
	
	//Vibrator vibrator;
	
	public MesaAdapter(Context context, List<Mesa> listaMesa) {
		//vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
		mInflater = LayoutInflater.from(context);
		this.listaMesas = listaMesa;
		this.tempoAviso = Sessao.SESSAO.getTempoAviso(context);
	}	
	
	static class ViewHolder {		
		TextView txtMesa;
		TextView txtUltimoAtendimento;		
	}

	@Override
	public int getCount() {
		return listaMesas.size();
	}

	@Override
	public Object getItem(int position) {
		return listaMesas.get(position);
	}

	@Override
	public long getItemId(int position) {		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
			
		
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.lista_mesas_abertas, null);
			holder = new ViewHolder();
 
			holder.txtMesa = (TextView) convertView.findViewById(R.listaMesasAbertas.txtMesa);			
			holder.txtUltimoAtendimento = (TextView) convertView.findViewById(R.listaMesasAbertas.txtUltimoAtendimento);
 
			convertView.setTag(holder);
 
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
 
		Mesa mesa = listaMesas.get(position);
 
		holder.txtMesa.setText(mesa.xMesa);
		holder.txtUltimoAtendimento.setText(mesa.horaUltAtendimento);	
		
		if (Float.valueOf(holder.txtUltimoAtendimento.getText().toString().substring(
				holder.txtUltimoAtendimento.getText().toString().indexOf(",")+4,
				holder.txtUltimoAtendimento.getText().toString().lastIndexOf("m")))>tempoAviso) {		          
			holder.txtUltimoAtendimento.setTextColor(Color.RED);
			//vibrator.vibrate(100);			
		} else {
			holder.txtUltimoAtendimento.setTextColor(Color.parseColor("#999999"));			
		}		
		
		//convertView.setBackgroundResource(R.drawable.bgmesa);
		
		/*if (position == PositionSelected) {
			convertView.setBackgroundResource(R.drawable.bgmesa_clicada);
		} else {
			convertView.setBackgroundResource(R.drawable.bgmesa);
		}*/
		
		return convertView;
	}
			

}
